function e = emax(p,q)
% emax(p,q) gives the highest Afriat Efficiency parameter such
% that the data satisfy GARP using a binary search procedure
%
% REQUIRES egarp
%
%GARP-website: http://www.revealedpreferences.org
%Aug 6, 2012

%check for garp consistency: yes => e = 1
if garp(p,q)==1;
    e = 1;
    
else
    
    eupper = 1;% unfeasible upper bound
    elower = 0;% feasible lower bound
    
    while (eupper-elower)/elower >= 0.00000000001 %change this if accuracy is to be changed
        
        eevaluate = (eupper+elower)/2;%evaluation point
        
        passevaluate = garpe(p,q,eevaluate);%evaluate this point
        
        if passevaluate == 1 %if it passes garp
            elower = eevaluate;%new lower bound = evaluation point
            
        elseif passevaluate == 0 %if it does not satisfy garp
            eupper = eevaluate;%new upper bound = evaluation point
        end
    end
        e = eevaluate;
end
    
disp('------------------------------------------------------------------');
disp(['Afriat critical cost index: ', num2str(e)]);
disp('------------------------------------------------------------------');
